﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using System.Collections.ObjectModel;

namespace ElkiTools.SP2010.SoapExtensions.Managers
{
    /*
     * --------------------------------------------------------------------
     * Code de l'extension par Stefan Gossner
     * --------------------------------------------------------------------
     * http://blogs.technet.com/b/stefan_gossner/archive/2009/08/26/how-to-deal-with-invalid-characters-in-soap-responses-from-asp-net-web-services.aspx
     * http://archive.msdn.microsoft.com/XmlCleanupSoapExtens
     * 
     * --------------------------------------------------------------------
     * Mise en oeuvre du SPWebConfigModification
     * --------------------------------------------------------------------
     * http://msdn.microsoft.com/fr-fr/library/bb861909(v=office.14).aspx
     * 
     * --------------------------------------------------------------------
     * Outil de tests
     * --------------------------------------------------------------------
     * https://wcms.codeplex.com/
    */

    public static class WebConfigModifManager
    {
        public const string CST_soapExtType = "StefanG.SoapExtensions.XmlCleanupSoapExtension, ElkiTools.SP2010.SoapExtensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=78d3d6a0b02f0f41";
        public static readonly string CST_SPWebConfigModifName = string.Format("add[@type='{0}'][@priority='1'][@group='0']", CST_soapExtType);
        public const string CST_SPWebConfigModifPath = "configuration/system.web/webServices/soapExtensionTypes";
        public const string CST_Farm_SPWebConfigModifOwner = @"a4805292-c6e5-47be-844c-a90768a7206d"; /* = FeatureId */
        public const string CST_WebApp_SPWebConfigModifOwner = @"737d5ebc-a1d6-4753-9048-ed20e73265ab"; /* = FeatureId */
        public static readonly string CST_SPWebConfigModifValue = string.Format("<add type=\"{0}\" priority=\"1\" group=\"0\" />", CST_soapExtType);

        public static void Add_SPWebConfigModif_Entries(Collection<SPWebConfigModification> wcModifs, string owner)
        {
            /* Les modifications à apporter au Web.Config doit permettre d'obtenir la section suivante
             * -----------------------------------------------------------------------------------------------
             * <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
             * <configuration> 
             *      <system.web> 
             *          <webServices> 
             *              <soapExtensionTypes> 
             *                  <add type="StefanG.SoapExtensions.XmlCleanupSoapExtension, iBP.EspaceCollab.SoapExtensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0bfe4c804fa1fbe9" priority="1" group="0" /> 
             *              </soapExtensionTypes> 
             *          [...]            
             */

            //================================================================================================
            // Création des SPWebServiceModifications
            // On considère que le noeud configuration/system.web existe
            //================================================================================================

            //------------------------------------------------------------------------------------------------
            // ==> webServicesModification
            #region configuration/system.web/webServices
            //------------------------------------------------------------------------------------------------
            wcModifs.Add(new SPWebConfigModification()
            {
                Path = "configuration/system.web",
                Name = "webServices",
                Sequence = 0,
                Owner = owner,
                Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
                Value = "<webServices/>"
            });
            //------------------------------------------------------------------------------------------------
            #endregion configuration/system.web/webServices
            //------------------------------------------------------------------------------------------------

            //------------------------------------------------------------------------------------------------
            // ==> soapExtensionTypesModification
            #region configuration/system.web/webServices/soapExtensionTypes
            //------------------------------------------------------------------------------------------------
            wcModifs.Add(new SPWebConfigModification()
            {
                Path = "configuration/system.web/webServices",
                Name = "soapExtensionTypes",
                Sequence = 0,
                Owner = owner,
                Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
                Value = "<soapExtensionTypes/>"
            });
            //------------------------------------------------------------------------------------------------
            #endregion configuration/system.web/webServices/soapExtensionTypes
            //------------------------------------------------------------------------------------------------

            //------------------------------------------------------------------------------------------------
            // ==> leafModification
            #region configuration/system.web/webServices/soapExtensionTypes/add type=...
            //------------------------------------------------------------------------------------------------
            wcModifs.Add(new SPWebConfigModification()
            {
                Path = CST_SPWebConfigModifPath,
                Name = CST_SPWebConfigModifName,
                Sequence = 0,
                Owner = owner,
                Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
                Value = CST_SPWebConfigModifValue
            });
            //------------------------------------------------------------------------------------------------
            #endregion configuration/system.web/webServices/soapExtensionTypes/add type=...
            //------------------------------------------------------------------------------------------------            
        }

        public static void Remove_SPWebConfigModif_Entries(Collection<SPWebConfigModification> wcModifs, string owner)
        {
            // Recherche des modifs à supprimer
            List<SPWebConfigModification> webConfModifsToDelete =
                wcModifs
                .Where(modif => owner.Equals(modif.Owner, StringComparison.InvariantCultureIgnoreCase))
                .ToList();

            foreach (SPWebConfigModification item in webConfModifsToDelete)
            {
                wcModifs.Remove(item);
            }
        }
    }
}
